package com.neutech.maplestory.util;

import com.neutech.maplestory.constant.Constant;
import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.Player;

import java.io.BufferedInputStream;
import java.io.FileNotFoundException;

public class MyMusicThread extends Thread{
    /**
     * 播放器类
     */
    private Player player;
    /**
     * mp3文件的路径（相对路径）
     */
    private String musicPath;
    /**
     * 是否循环
     */
    private boolean loop;
    public MyMusicThread() {

    }
    /**
     *
     * @param musicName MP3文件的相对路径
     */
    public MyMusicThread(String musicName) {
        this.musicPath = Constant.PRE_MP3 + musicName;
    }
    /**
     *
     * @param musicName MP3文件的相对路径
     * @param loop 是否循环
     */
    public MyMusicThread(String musicName,boolean loop) {
        this.musicPath = Constant.PRE_MP3 + musicName;
        this.loop = loop;
    }

    @Override
    public void run() {
        try {
            if(loop) {
                while(true) {
                    play();
                }
            }else {
                play();
            }
        } catch (FileNotFoundException | JavaLayerException e) {
            e.printStackTrace();
        }
    }
    private void play() throws FileNotFoundException, JavaLayerException{
        BufferedInputStream buffer = new BufferedInputStream(MyMusicThread.class.getClassLoader().getResourceAsStream(musicPath));
        // 创建播放器类
        player = new Player(buffer);
        // 播放
        player.play();
    }

    /*public static void main(String[] args) {
        new MyMusicThread("bgm.mp3", true).start();
    }*/
}
